﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataModel.Core
{
    public interface IFileRepository
    {
        string AddFile(string FileName);
    }
    public class DefaultFileRepository : IFileRepository
    {
        private readonly string Directory;
        public DefaultFileRepository(string Directory)
        {
            if (!System.IO.Directory.Exists(Directory))
                throw new System.IO.DirectoryNotFoundException(string.Format("Путь {0} не обнаружен", Directory));
            this.Directory = Directory;
        }

        public string AddFile(string FileName)
        {
            string NewFileName = FileUtils.GetFileName(FileName, Directory);
            System.IO.File.Copy(FileName, NewFileName);
            return NewFileName;
        }
    }
    internal static class FileUtils
    {
        public static string GetFileName(string FullFileName, string Directory)
        {
            string FileName = System.IO.Path.GetFileNameWithoutExtension(FullFileName);
            string Extension = System.IO.Path.GetExtension(FullFileName);
            string NewFileName =  System.IO.Path.Combine(Directory, FileName + Extension);  
            if (System.IO.File.Exists(NewFileName))
                NewFileName = System.IO.Path.Combine(Directory, FileName + DateTime.Now.ToString("ddMMyyyyHHmmss") + Extension);
            return NewFileName;
        }
    }
}
